//
//  ZDMineViewController.m
//  1012GiftTips
//
//  Created by Apple on 15/10/16.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDMineViewController.h"
#import "ZDJumpViewController.h"
#import "ZDSettingViewController.h"
#import "UIView+ZUtility.h"
#import "ZDTopView.h"
#import "ZDSettingItem.h"
#import "ZDSettingItemArrow.h"
#import "ZDSettingItemCell.h"
#import "ZDSectionHeaderView.h"

#define ZDTopViewHeight 200


@interface ZDMineViewController () <UINavigationControllerDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

//组模型
@property (nonatomic, strong) NSArray *groups;

@property (nonatomic, strong) ZDSectionHeaderView *sectionHeaderView;

@end

@implementation ZDMineViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //关闭自动调整ScrollViewInsets
    //The default value of this property is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar  该属性默认值是YES，即允许控制器根据status bar，navigation bar, and toolbar or tab bar占据的屏幕空间来自动调整控制器scrollView的插入----目的是为了使显示内容不被这些bar遮挡，比如如果控制器的tableView是从(0, 0)位置开始的，那么，控制器会自动调整tableView的contentInsert.top等于64，也即是tableView的contentOffset.y等于-64。
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置代理
    self.navigationController.delegate = self;
    
    //关闭滚动时隐藏导航条
    self.navigationController.hidesBarsOnSwipe = NO;
    
    //设置返回按钮
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;

    //设置导航条按钮的颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    //设置navigationBar背景透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //覆盖navigationBar底部的黑色线(黑色阴影)
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    
    UIBarButtonItem *messageItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Me_message"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBtnClick)];
    
    UIBarButtonItem *remindItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Me_remind_item"] style:UIBarButtonItemStyleDone target:self action:@selector(remindBtnClick)];
    
    self.navigationItem.leftBarButtonItems = @[messageItem, remindItem];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Me_settings"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClick)];
    
  
    
//    self.tableView.frame = CGRectMake(0, 0, self.view.width, self.view.height);
    
    
    ZDTopView *topView = [[ZDTopView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, ZDTopViewHeight) andBackgroundImageName:@"Me_ProfileBackground.jpg" andHeadImageName:@"me_avatar_boy" andTitle:@"playboy" andSubTitle:nil];
    
    topView.headImageClickBlock = ^{
        
        NSLog(@"点击了头像");
    };
    
    topView.scrollView = self.tableView;
    
    [self.view addSubview:topView];
}


//FIXME: 子控制器返回时，navigationBar有黑条？  验证自定义导航条能否解决这个问题
- (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.navigationController.navigationBar.alpha = 1.0;
    
    // 如果进入的是当前视图控制器
    if (viewController == self) {
//        // 背景设置为黑色
//        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//        // 透明度设置为1.0
//        self.navigationController.navigationBar.alpha = 1.0;
        // 设置为半透明
        self.navigationController.navigationBar.translucent = YES;
    } else {
        // 进入其他视图控制器
//        self.navigationController.navigationBar.alpha = 1.0;
//        // 背景颜色设置为系统默认颜色
//        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        self.navigationController.navigationBar.translucent = NO;
    }
}


- (void)leftBtnClick
{
    NSLog(@"leftBtnClick");
    
    ZDJumpViewController *jumpVc = [[ZDJumpViewController alloc] init];
    
    jumpVc.title = @"我的消息";
    
    [self.navigationController pushViewController:jumpVc animated:YES];
}

- (void)rightBtnClick
{
    NSLog(@"rightBtnClick");
    
    ZDSettingViewController *settingVc = [[ZDSettingViewController alloc] init];
    
    settingVc.title = @"设置";
    
    [self.navigationController pushViewController:settingVc animated:YES];
}

- (void)remindBtnClick
{
    NSLog(@"remindBtnClick");
    
    ZDJumpViewController *jumpVc = [[ZDJumpViewController alloc] init];
    
    jumpVc.title = @"提醒";
    
    [self.navigationController pushViewController:jumpVc animated:YES];
}

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 3;
    }
    else
    {
        if (self.sectionHeaderView.type == ZDButtonTypeLeft)
        {
            return 1;
        }
        else
        {
            return 10;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        static NSString *ID = @"cell";
        
        ZDSettingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (!cell)
        {
            cell = [[ZDSettingItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        //    cell.textLabel.text = [NSString stringWithFormat:@"cell%ld", indexPath.row];
        
        cell.item = self.groups[indexPath.row];
        
        return cell;
    }
    else
    {
        static NSString *reuseID = @"sectionCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
        
        if (!cell)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
        }
        
        //    cell.textLabel.text = [NSString stringWithFormat:@"cell%ld", indexPath.row];
        
        cell.imageView.image = [UIImage imageNamed:@"Share_WeChatSessionIcon"];
        cell.textLabel.text = @"5分钟，教你搞定美味营养早餐";
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    
    view.backgroundColor = ZDColor(235, 235, 242);
    
    return view;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0;
    }
    else
    {
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 11;
    }
    else
    {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 44;
    }
    else
    {
        return 88;
    }
}



#pragma mark 懒加载
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
//        _tableView.sectionFooterHeight = 11; //这里设置的是全局FooterHeight和HeaderHeight，没法定制
//        _tableView.sectionHeaderHeight = 44;
        
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}


- (NSArray *)groups
{
    if (!_groups)
    {
        ZDSettingItem *item11 = [ZDSettingItemArrow settingItemWithIcon:@"Me_credit" andTitle:@"我的积分" andLabel:@"5"];
        ZDSettingItem *item12 = [ZDSettingItemArrow settingItemWithIcon:@"Me_taobaoOrder" andTitle:@"我的订单"];
        ZDSettingItem *item13 = [ZDSettingItemArrow settingItemWithIcon:@"Me_coupon" andTitle:@"我的礼券"];
        
        _groups = @[item11, item12, item13];
    }
    
    return _groups;
}


- (ZDSectionHeaderView *)sectionHeaderView
{
    if (!_sectionHeaderView)
    {
        _sectionHeaderView = [ZDSectionHeaderView sectionHeaderView];
//        _sectionHeaderView.type = ZDButtonTypeLeft;
        
        __weak typeof(self) weakSelf = self;
        
        _sectionHeaderView.reloadDataBlock = ^{
        
            [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
        };
    }
    
    return _sectionHeaderView;
}

@end
